<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorName">
        <el-input
            v-model="queryParams.doctorName"
            placeholder="请输入医生名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格：展示姓名，保留ID列（可选，用于参考） -->
    <el-table v-loading="loading" :data="behindDocComplaintList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="投诉表id" align="center" prop="complaintId" />-->
      <!-- 显示用户姓名，隐藏用户ID（或保留ID列） -->
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <!-- <el-table-column label="用户id" align="center" prop="userId" v-show="false" /> -->
      <!-- 显示医生姓名，隐藏医生ID（或保留ID列） -->
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
<!--       <el-table-column label="医生id" align="center" prop="doctorId" v-show="false" />-->
      <el-table-column label="投诉原因" align="center" prop="complaintReason" />
      <el-table-column label="投诉内容" align="center" prop="complaintInfo" />
      <el-table-column label="投诉状态" align="center" prop="complaintState">
        <template #default="scope">
          <el-tag :type="getComplaintStateTagType(scope.row.complaintState)">
            {{ getComplaintStateText(scope.row.complaintState) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="danger" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['behindComplaint:behindDocComplaint:edit']">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 处理对话框：展示姓名，提交ID -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="behindDocComplaintRef" :model="form" :rules="rules" label-width="80px">
        <!-- 新增：用户姓名（仅展示，禁用） -->
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" disabled />
        </el-form-item>
        <!-- 新增：医生姓名（仅展示，禁用） -->
        <el-form-item label="医生姓名" prop="doctorName">
          <el-input v-model="form.doctorName" disabled />
        </el-form-item>
        <!-- 保留用户ID（隐藏或只读，确保提交给后台） -->
        <el-form-item label="用户id" prop="userId" v-show="false">
          <el-input v-model="form.userId" />
        </el-form-item>
        <!-- 保留医生ID（隐藏或只读，确保提交给后台） -->
        <el-form-item label="医生id" prop="doctorId" v-show="false">
          <el-input v-model="form.doctorId" />
        </el-form-item>
        <el-form-item label="投诉原因" prop="complaintReason">
          <el-input v-model="form.complaintReason" disabled/>
        </el-form-item>
        <el-form-item label="投诉内容" prop="complaintInfo">
          <el-input v-model="form.complaintInfo" type="textarea" disabled />
        </el-form-item>
        <el-form-item label="投诉状态" prop="complaintState">
          <el-radio-group v-model="form.complaintState" class="no-border-radio">
            <el-radio label="0">已处理</el-radio>
            <el-radio label="1">待处理</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确认处理</el-button>
          <el-button @click="cancel">稍后处理</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BehindDocComplaint">
import { listBehindDocComplaint, getBehindDocComplaint, delBehindDocComplaint, addBehindDocComplaint, updateBehindDocComplaint } from "@/api/behindComplaint/behindDocComplaint"

const { proxy } = getCurrentInstance()

const behindDocComplaintList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("投诉处理")

// 表单数据：新增userName和doctorName用于展示，保留userId和doctorId用于提交
const data = reactive({
  form: {
    complaintId: null,
    userId: null,       // 提交给后台的用户ID
    userName: null,     // 仅用于展示的用户姓名
    doctorId: null,     // 提交给后台的医生ID
    doctorName: null,   // 仅用于展示的医生姓名
    complaintReason: null,
    complaintInfo: null,
    complaintState: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    doctorId: null,
    complaintReason: null,
    complaintInfo: null,
    complaintState: null
  },
  rules: {
    // 移除禁用字段的校验，只校验状态
    complaintState: [
      { required: true, message: "请选择投诉状态", trigger: "change" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询投诉处理列表 */
function getList() {
  loading.value = true
  listBehindDocComplaint(queryParams.value).then(response => {
    // 后端返回的数据包含userName和doctorName，直接绑定到表格
    behindDocComplaintList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    complaintId: null,
    userId: null,
    userName: null,
    doctorId: null,
    doctorName: null,
    complaintReason: null,
    complaintInfo: null,
    complaintState: null
  }
  proxy.resetForm("behindDocComplaintRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.complaintId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 处理按钮操作：获取详情并赋值姓名 */
function handleUpdate(row) {
  reset()
  const _complaintId = row.complaintId || ids.value
  getBehindDocComplaint(_complaintId).then(response => {
    const data = response.data
    // 赋值ID（用于提交）和姓名（用于展示）
    form.value = {
      complaintId: data.complaintId,
      userId: data.userId,
      userName: data.userName,  // 后端返回的用户姓名
      doctorId: data.doctorId,
      doctorName: data.doctorName,  // 后端返回的医生姓名
      complaintReason: data.complaintReason,
      complaintInfo: data.complaintInfo,
      complaintState: data.complaintState
    }
    open.value = true
    title.value = "投诉处理"
  })
}

/** 提交按钮：提交的是ID，姓名不参与提交 */
function submitForm() {
  proxy.$refs["behindDocComplaintRef"].validate(valid => {
    if (valid) {
      // 表单提交的是form.value，包含userId和doctorId（ID），不包含userName和doctorName
      updateBehindDocComplaint(form.value).then(response => {
        proxy.$modal.msgSuccess("处理成功")
        open.value = false
        getList()
      })
    }
  })
}

/** 其他函数保持不变 */
function handleDelete(row) { /* ... */ }
function handleExport() { /* ... */ }

const getComplaintStateText = (state) => {
  switch (state) {
    case "0": return '已处理';
    case "1": return '待处理';
    default: return '未知状态';
  }
};

const getComplaintStateTagType = (state) => {
  switch (state) {
    case "0": return 'success';
    case "1": return 'warning';
    default: return 'danger';
  }
};

getList()
</script>